
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');

const moment=require('moment');

const db=require(path.join(__dirname,"../configs/database.js"));

const base=require(path.join(__dirname,"./baseController.js"));

module.exports.workAdd=function(req,res){


   // 显示班级
   db.table('class').select().then(function(data){

     xtpl.renderFile(

         path.join(__dirname,"../views/admin/work_add.html"),
         {

         	array:data,
         },
         function(err,content){
         	res.setHeader("Content-Type","text/html;charset=utf-8");
         	res.end(content);
         }

     	);
   });

};

module.exports.workAddData=function(req,res){


  let where=`work_title='${req.body.work_title}'`;
  db.table('homework').where(where).find().then(function(data){
   if(JSON.stringify(data)!="{}"){
       base.shoeMsg(res,'作业已经存在','/work/work_list');
       return false;

   }else{
      let data={
         "work_title":req.body.work_title,
         "work_time":(Date.parse(moment(req.body.work_time))/1000),
         "work_content":req.body.work_content,
         "class_id":req.body.class_id
            
      };
      db.table('homework').add(data).then(function(affectRows){
        res.redirect('/work/work_list');
        return false;
      }).catch(function(err){
         if(err){
         	return console.log(err);
         }

      });

   }
  }); 

  
};


// 作业列表

module.exports.workList=function(req,res){

  
  const page=parseInt(req.query.page||'1');
  const limit=parseInt(req.query.limit||'12');
  const start=(page-1)*limit;

  db.table("homework").count("*").then(function(count){

     const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
     const totalPageArray=[];
     
     for(var i=1;i<=totalPage;i++){
         totalPageArray.push(i);
     }

  db.table("homework").join("pre_class on pre_homework.class_id = pre_class.class_id").order("work_id DESC").limit(start,limit).select().then(function(data){
   xtpl.renderFile(
    path.join(__dirname,"../views/admin/work_list.html"),
    {
    	array:data,
    	totalPage:totalPage,
        totalPageArray:totalPageArray,
        page:page
    },
    function(err,content){
        if(err){
           return console.log(err);
        }
        res.setHeader("Content-Type","text/html;charset=utf-8");
        res.end(content);
    }

  	);
     });


  });

};

module.exports.workEdite=function(req,res){

   // 对应的班级要显示 对应作业要显示
   
  let work_id=req.params.work_id;

  let where=`work_id=${work_id}`;

  db.table("homework").where(where).find().then(function(data){


     db.table('class').select().then(function(cData){

           
           if(JSON.stringify(data)=="{}"){

               base.showMsg(res,'该作业不存在','/work/work_list');
               return false;

           }else{



              data.work_time=moment(data.work_time*1000).format("YYYY-MM-DD");
              xtpl.renderFile(

                path.join(__dirname,"../views/admin/work_edite.html"),
                {


                	crr:data,
                	arr:cData
                },
                function(err,content){
                	res.setHeader("Content-Type","text/html;charset=utf-8");
                	res.end(content);
                }


              	);


           }


     });


  });

};


module.exports.workEditeAdd=function(req,res){

	 let work_id=req.params.work_id;
	 let where=`work_id=${work_id}`;
	 db.table('homework').where(where).find().then(function(data){

       if(JSON.stringify(data)=="{}"){
            base.showMsg(res,'该学生不存在','/work/work_list');
            return false;

       }else{

           let check=`work_title='${req.body.work_title}' AND work_id!=${work_id}`;



           db.table("homework").where(check).find().then(function(data){

             if(JSON.stringify(data)!="{}"){

                base.showMsg(res,'作业已经存在');
                return false;

             }else{

                 let data={
                     "work_title":req.body.work_title,
                     "work_time":(Date.parse(moment(req.body.work_time))/1000),
                     "work_content":req.body.work_content,
                     "class_id":req.body.class_id
            
                     };

                 db.table("homework").where(`work_id=${work_id}`).update(data).then(function(){

                     res.redirect('/work/work_list');
                     return false;
                 }).catch(function(err,content){

                 	 if(err){

                 	 	base.shoeMsg(res,'更新失败','/work/work_list');
                 	 	return false;
                 	 }
                 });

             }


           });



       }


	 });
 
};

module.exports.workDelete=function(req,res){
	
 let work_id=req.params.work_id||0;
 let where=`work_id=${work_id}`;

db.table('homework').where(where).delete().then(function(workRows){

  base.showMsg(res,'删除成功','/work/work_list');
  return false;

}).catch(function(err){
       base.showMsg(res,'删除失败','/work/work_list');
       return false;

});


}